// API接口配置
const BASE_URL = 'https://your-api-domain.com/api'

// 请求方法
const request = (url, method, data = {}) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: BASE_URL + url,
      method: method,
      data: data,
      header: {
        'Content-Type': 'application/json',
        'Authorization': wx.getStorageSync('token') || ''
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data)
        } else {
          reject(res.data)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 用户相关API
const userApi = {
  // 登录
  login: (code) => request('/user/login', 'POST', { code }),
  
  // 获取用户信息
  getUserInfo: () => request('/user/info', 'GET'),
  
  // 更新用户信息
  updateUserInfo: (data) => request('/user/update', 'POST', data),
  
  // 获取用户积分
  getUserPoints: () => request('/user/points', 'GET')
}

// 预约相关API
const appointmentApi = {
  // 创建预约
  createAppointment: (data) => request('/appointment/create', 'POST', data),
  
  // 获取预约列表
  getAppointments: (page = 1, limit = 10) => request(`/appointment/list?page=${page}&limit=${limit}`, 'GET'),
  
  // 取消预约
  cancelAppointment: (id) => request(`/appointment/cancel/${id}`, 'POST'),
  
  // 获取预约详情
  getAppointmentDetail: (id) => request(`/appointment/detail/${id}`, 'GET')
}

// 服务相关API
const serviceApi = {
  // 获取服务列表
  getServices: () => request('/service/list', 'GET'),
  
  // 获取服务详情
  getServiceDetail: (id) => request(`/service/detail/${id}`, 'GET'),
  
  // 获取热门服务
  getHotServices: () => request('/service/hot', 'GET')
}

// 医生相关API
const doctorApi = {
  // 获取医生列表
  getDoctors: () => request('/doctor/list', 'GET'),
  
  // 获取医生详情
  getDoctorDetail: (id) => request(`/doctor/detail/${id}`, 'GET'),
  
  // 获取医生评价
  getDoctorReviews: (id) => request(`/doctor/reviews/${id}`, 'GET')
}

// 订单相关API
const orderApi = {
  // 创建订单
  createOrder: (data) => request('/order/create', 'POST', data),
  
  // 获取订单列表
  getOrders: (page = 1, limit = 10) => request(`/order/list?page=${page}&limit=${limit}`, 'GET'),
  
  // 获取订单详情
  getOrderDetail: (id) => request(`/order/detail/${id}`, 'GET'),
  
  // 取消订单
  cancelOrder: (id) => request(`/order/cancel/${id}`, 'POST')
}

// 优惠券相关API
const couponApi = {
  // 获取优惠券列表
  getCoupons: () => request('/coupon/list', 'GET'),
  
  // 领取优惠券
  receiveCoupon: (id) => request(`/coupon/receive/${id}`, 'POST'),
  
  // 获取我的优惠券
  getMyCoupons: () => request('/coupon/my', 'GET')
}

module.exports = {
  userApi,
  appointmentApi,
  serviceApi,
  doctorApi,
  orderApi,
  couponApi,
  request
}